* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization
* The Effects of Signals on Vaccine Knowledge by Bracelet Color
* **************************************************************************** *
/*
** Purpose:    The Effects of Signals on Vaccine Knowledge by Bracelet Color

*/
* **************************************************************************** *
* **************************************************************************** *


	use  "${Replicate_SocialSignals_dtaInter}/FirstOrderBeliefs_Data.dta", clear

	* Append Second-Order Beliefs
	append using "${Replicate_SocialSignals_dtaInter}/SecondOrderBeliefs_Data.dta", gen(Append_SecondOrder)


	* Drop the control arm clinics:
	drop if treat1 == 1

	gen     baby_color_yellow =.
	* Zero if green:
	replace baby_color_yellow = 0            if baby_color1 == 1
	* One if yellow
	replace baby_color_yellow = 1            if baby_color2 == 1


	* Generate the yellow bracelet variable taking into account the second order beliefs
	gen     yellow_own = 1        if yellow_bracelet_own==1
	replace yellow_own = 0        if green_bracelet_own==1


	tab      yellow_own
	tab      yellow_own green_bracelet_own
	tab      yellow_own yellow_bracelet_own

	* Generate one combined indicator for green:
	gen     yellow_bracelet = .
	replace yellow_bracelet = baby_color_yellow            if baby_color_yellow !=.
	replace yellow_bracelet = yellow_own                   if yellow_own !=.

	lab var yellow_bracelet "Yellow Bracelet"

	summ    correct_vacc  baby_color2   yellow_bracelet beliefs_match

	drop    anc*
	tab     arm_anc,     gen(anc)

	drop edu1 edu2 edu3 birth1 birth2 birth3 birth4 birth5 anc1 anc2 anc3 anc4

	drop   *_first*  
	tab     birth_order, gen(birth)
	tab     educ, gen(edu)
	tab     arm_anc, gen(anc)




	* ------------------------------------------------------------------------------
	* First-Order Beliefs: Demean the controls:

	local   ControlVars = " mother_age_w01 age_u1 edu2 edu3 farm birth2 birth3 birth4 birth5 relate anc2 anc3 anc4"

	foreach control of local ControlVars {
		gen    `control'_first_107  = `control'            if age_u1>=107    & age_u1<274     & yellow_bracelet !=.
		gen    `control'_first_274  = `control'            if age_u1>=274    & age_u1<=365    & yellow_bracelet !=.
	}


	* Demean First Order Beliefs Controls :
	local   ControlVars = " mother_age_w01  age_u1 edu2 edu3 farm birth2 birth3 birth4 birth5 relate anc2 anc3 anc4"

	* For 3.5 - 12 months
	foreach control of local ControlVars  {
		sum    `control'_first_107   if age_u1>=107 & age_u1<274  & yellow_bracelet !=. & intervention_arm==2, detail
		gen    `control'_first_107_dm    = `control'_first_107 - `r(mean)'      if age_u1>=107 & age_u1<274  & yellow_bracelet !=.
	}


	bys clinic: egen mean_p3 = mean(penta3_u1)                     if age_u1>=107&age_u1< 274

	foreach var in p3  {
		 sum mean_`var'       if age_u1>=107&age_u1< 274 & intervention_arm==2, detail
		gen `var'_dm = mean_`var' - `r(mean)'         if age_u1>=107&age_u1< 274
	}



	* For 9 - 12 months
	foreach control of local ControlVars  {
		sum    `control'_first_274    if age_u1>=274 & age_u1<=365 & yellow_bracelet !=. & intervention_arm==2, detail
		gen    `control'_first_274_dm     = `control'_first_274 - `r(mean)'    if age_u1>=274 & age_u1<=365 & yellow_bracelet !=.
	}

	* Generate clinic-level mean Measles 1 immunization rates
	bys   clinic: egen mean_m1 = mean(measles1_u1)    if age_u1>=274 & age_u1<=365

	foreach var in m1  {
		sum mean_`var' if age_u1>=274 & age_u1<=365 & intervention_arm==2, detail
		gen `var'_dm           = mean_`var'   -  `r(mean)'   if age_u1>=274 & age_u1<=365 
	}



	save  "${Replicate_SocialSignals_dtaFinal}/KnowledgeTable_FirstOrderBeliefs_analysis.dta", replace


	* ---------------------------------------------------------------------------- *
	* 1.) The Effects of Signals on Vaccine Knowledge by Bracelet Color - with Controls
	* ---------------------------------------------------------------------------- *

	use   "${Replicate_SocialSignals_dtaFinal}/KnowledgeTable_FirstOrderBeliefs_analysis.dta", clear


	* First-Order Beliefs Controls:
	* For 3.5 - 12 months
	local FO_Controls_107 = " mother_age_w01_first_107_dm age_u1_first_107_dm edu2_first_107_dm edu3_first_107_dm farm_first_107_dm birth2_first_107_dm birth3_first_107_dm birth4_first_107_dm birth5_first_107_dm relate_first_107_dm p3_dm"

	local FO_ANC_107      = " anc2_first_107_dm anc3_first_107_dm anc4_first_107_dm "

	summ `FO_Controls_107' `FO_ANC_107'

	* For 9 - 12 months
	local FO_Controls_274 = " mother_age_w01_first_274_dm age_u1_first_274_dm edu2_first_274_dm edu3_first_274_dm farm_first_274_dm  birth2_first_274_dm birth3_first_274_dm birth4_first_274_dm birth5_first_274_dm relate_first_274_dm m1_dm"

	local FO_ANC_274      = " anc2_first_274_dm anc3_first_274_dm  anc4_first_274_dm "

	summ `FO_Controls_274' `FO_ANC_274'




	summ  correct_vacc   intervention_arm    yellow_bracelet    `FO_Controls_107' `FO_ANC_107'       if age_u1>= 107 & age_u1<274



	* Interaction With Controls:
	eststo clear
	foreach category in correct under over {
	areg    `category'_vacc  ib2.intervention_arm##ib0.yellow_bracelet    `FO_Controls_107' `FO_ANC_107'       if age_u1>= 107 & age_u1<274,  absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
	test   3.intervention_arm = 4.intervention_arm
	estadd scalar signal4_signal5 = r(p)
	test   3.intervention_arm + 3.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet = 4.intervention_arm + 4.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
	estadd scalar signal4_ng_signal5_ng = r(p)
	test   1.yellow_bracelet = 3.intervention_arm + 3.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
	estadd scalar UI_ng_signal4_ng = r(p)
	test   1.yellow_bracelet = 4.intervention_arm + 4.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
	estadd scalar UI_ng_signal5_ng = r(p)
	estadd scalar C_mean  = _b[_cons]
	estadd local Obs      = "`e(N)'"
	estadd local controls = "Yes"
	eststo model_`category'_35mo
	}

	foreach category in correct under over {
	areg    `category'_vacc  ib2.intervention_arm##ib0.yellow_bracelet    `FO_Controls_274' `FO_ANC_274'        if age_u1>= 274 & age_u1<=365,  absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
	test   3.intervention_arm = 4.intervention_arm
	estadd scalar signal4_signal5 = r(p)
	test   3.intervention_arm + 3.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet = 4.intervention_arm + 4.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
	estadd scalar signal4_ng_signal5_ng = r(p)
	test   1.yellow_bracelet = 3.intervention_arm + 3.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
	estadd scalar UI_ng_signal4_ng = r(p)
	test   1.yellow_bracelet = 4.intervention_arm + 4.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
	estadd scalar UI_ng_signal5_ng = r(p)
	estadd scalar C_mean  = _b[_cons]
	estadd local Obs      = "`e(N)'"
	estadd local controls = "Yes"
	eststo model_`category'_9mo
	}




	esttab  ///
	model_correct_35mo model_under_35mo model_over_35mo                 ///
	model_correct_9mo  model_under_9mo  model_over_9mo                  ///
	using "${Replicate_SocialSignals_Tables}/Table_GuessingBehavior_raw.tex", ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule "                         ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{6}{c}{\textbf{Number of vaccines of other children}}\\" ///
	"                                                 & \multicolumn{1}{c}{\textbf{Correct}} & \multicolumn{1}{c}{\textbf{Underestimate}} & \multicolumn{1}{c}{\textbf{Overestimate}} & \multicolumn{1}{c}{\textbf{Correct}}  & \multicolumn{1}{c}{\textbf{Underestimate}} & \multicolumn{1}{c}{\textbf{Overestimate}} \\" ///
	"\multicolumn{1}{l}{Vaccine take-up period}      & \multicolumn{3}{l}{3.5-9 months}  & \multicolumn{3}{l}{9-12 months} \\") ///
	scalars("C_mean             Mean Uninformative Bracelet "              ///
	"Obs               Observations"                              ///
	"controls          Controls"                                  ///
	"signal4_signal5   p(S4 Green = S5 Green)"                                ///
	"signal4_ng_signal5_ng p(S4 Yellow = S5 Yellow)"       ///
	"UI_ng_signal4_ng  p(UI Yellow = S4 Yellow)"            ///
	"UI_ng_signal5_ng  p(UI Yellow = S5 Yellow)")           ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	order(3.intervention_arm                    4.intervention_arm        ///
	3.intervention_arm#1.yellow_bracelet  4.intervention_arm#1.yellow_bracelet  ///
	1.yellow_bracelet)                                              ///
	drop(_cons *dm* )  nomtitles                                          ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                       ///
	addnotes("") label se  nobaselevels noconstant noobs nolines          ///
	posthead(\midrule) postfoot("\bottomrule \end{tabular}") tex          ///
	replace

	filefilter "${Replicate_SocialSignals_Tables}/Table_GuessingBehavior_raw.tex"  ///
	"${Replicate_SocialSignals_Tables}/Table_GuessingBehavior_Vaccines_InterYellow_FirstSecond_Controls.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_Tables}/Table_GuessingBehavior_raw.tex"



	* ----------------------------------------------------------------------------
	* outsheet p-values, with controls

	esttab  ///
	model_correct_35mo model_under_35mo model_over_35mo                 ///
	model_correct_9mo  model_under_9mo  model_over_9mo                  ///
	using "${Replicate_SocialSignals_Tables}/Table_GuessingBehavior_raw.tex", ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule "                         ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{6}{c}{\textbf{Number of vaccines of other children}}\\" ///
	"                                                 & \multicolumn{1}{c}{\textbf{Correct}} & \multicolumn{1}{c}{\textbf{Underestimate}} & \multicolumn{1}{c}{\textbf{Overestimate}} & \multicolumn{1}{c}{\textbf{Correct}}  & \multicolumn{1}{c}{\textbf{Underestimate}} & \multicolumn{1}{c}{\textbf{Overestimate}} \\" ///
	"\multicolumn{1}{l}{Vaccine take-up period}      & \multicolumn{3}{l}{3.5-9 months}  & \multicolumn{3}{l}{9-12 months} \\") ///
	scalars("C_mean             Mean Uninformative Bracelet "              ///
	"Obs               Observations"                              ///
	"controls          Controls"                                  ///
	"signal4_signal5   p(S4 Green = S5 Green)"                                ///
	"signal4_ng_signal5_ng p(S4 Yellow = S5 Yellow)"       ///
	"UI_ng_signal4_ng  p(UI Yellow = S4 Yellow)"            ///
	"UI_ng_signal5_ng  p(UI Yellow = S5 Yellow)")           ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	order(3.intervention_arm                    4.intervention_arm        ///
	3.intervention_arm#1.yellow_bracelet  4.intervention_arm#1.yellow_bracelet  ///
	1.yellow_bracelet)                                              ///
	drop(_cons *dm* )  nomtitles                                          ///
	sfmt(3) b(3) p(3)  ${StarsOpt}                                       ///
	addnotes("") label nobaselevels noconstant noobs nolines              ///
	posthead(\midrule) postfoot("\bottomrule \end{tabular}") tex          ///
	replace

	filefilter "${Replicate_SocialSignals_Tables}/Table_GuessingBehavior_raw.tex"  ///
	"${Replicate_SocialSignals_Tables}/pvalues/Table_GuessingBehavior_Vaccines_InterYellow_FirstSecond_Controls_pvals.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_Tables}/Table_GuessingBehavior_raw.tex"





	* ---------------------------------------------------------------------------- *
	* 2.) The Effects of Signals on Vaccine Knowledge by Bracelet Color - without controls
	* ---------------------------------------------------------------------------- *

	use   "${Replicate_SocialSignals_dtaFinal}/KnowledgeTable_FirstOrderBeliefs_analysis.dta", clear


	* For 3.5 - 12 months
	local FO_ANC_107 = " anc2_first_107_dm  anc3_first_107_dm  anc4_first_107_dm "

	summ `FO_ANC_107'


	* For 9 - 12 months
	local FO_ANC_274 = " anc2_first_274_dm  anc3_first_274_dm  anc4_first_274_dm "

	summ `FO_ANC_274'


	* Interaction Without  Controls:
	eststo clear
	foreach category in correct under over {
		areg    `category'_vacc  ib2.intervention_arm##ib0.yellow_bracelet   `FO_ANC_107'         if age_u1>= 107 & age_u1<274,  absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
		test   3.intervention_arm = 4.intervention_arm
		estadd scalar signal4_signal5 = r(p)
		test   3.intervention_arm + 3.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet = 4.intervention_arm + 4.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
		estadd scalar signal4_ng_signal5_ng = r(p)
		test   1.yellow_bracelet = 3.intervention_arm + 3.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
		estadd scalar UI_ng_signal4_ng = r(p)
		test   1.yellow_bracelet = 4.intervention_arm + 4.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
		estadd scalar UI_ng_signal5_ng = r(p)
		estadd scalar C_mean  = _b[_cons]
		estadd local Obs      = "`e(N)'"
		estadd local controls = "No"
		eststo model_`category'_35mo
	}

	foreach category in correct under over {
		areg    `category'_vacc  ib2.intervention_arm##ib0.yellow_bracelet  `FO_ANC_274'        if age_u1>= 274 & age_u1<=365,  absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
		test   3.intervention_arm = 4.intervention_arm
		estadd scalar signal4_signal5 = r(p)
		test   3.intervention_arm + 3.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet = 4.intervention_arm + 4.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
		estadd scalar signal4_ng_signal5_ng = r(p)
		test   1.yellow_bracelet = 3.intervention_arm + 3.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
		estadd scalar UI_ng_signal4_ng = r(p)
		test   1.yellow_bracelet = 4.intervention_arm + 4.intervention_arm#1.yellow_bracelet + 1.yellow_bracelet
		estadd scalar UI_ng_signal5_ng = r(p)
		estadd scalar C_mean  = _b[_cons]
		estadd local Obs      = "`e(N)'"
		estadd local controls = "No"
		eststo model_`category'_9mo
	}



	esttab  ///
	model_correct_35mo model_under_35mo model_over_35mo                 ///
	model_correct_9mo  model_under_9mo  model_over_9mo                  ///
	using "${Replicate_SocialSignals_ATables}/Table_GuessingBehavior_raw.tex", ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule "                         ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{6}{c}{\textbf{Number of vaccines of other children}}\\" ///
	"                                                 & \multicolumn{1}{c}{\textbf{Correct}} & \multicolumn{1}{c}{\textbf{Underestimate}} & \multicolumn{1}{c}{\textbf{Overestimate}} & \multicolumn{1}{c}{\textbf{Correct}}  & \multicolumn{1}{c}{\textbf{Underestimate}} & \multicolumn{1}{c}{\textbf{Overestimate}} \\" ///
	"\multicolumn{1}{l}{Vaccine take-up period}      & \multicolumn{3}{l}{3.5-9 months}  & \multicolumn{3}{l}{9-12 months} \\") ///
	scalars("C_mean             Mean Uninformative Bracelet "             ///
	"Obs               Observations"                             ///
	"controls          Controls"                                 ///
	"signal4_signal5   p(S4 Green = S5 Green)"             ///
	"signal4_ng_signal5_ng p(S4 Yellow = S5 Yellow)"       ///
	"UI_ng_signal4_ng  p(UI Yellow = S4 Yellow)"           ///
	"UI_ng_signal5_ng  p(UI Yellow = S5 Yellow)")          ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	order(3.intervention_arm                    4.intervention_arm        ///
	3.intervention_arm#1.yellow_bracelet  4.intervention_arm#1.yellow_bracelet  ///
	1.yellow_bracelet)                                              ///
	drop(_cons *dm*)  nomtitles                                           ///
	sfmt(3) b(3) p(3)  ${StarsOpt}                                        ///
	addnotes("") label nobaselevels noconstant noobs nolines              ///
	posthead(\midrule) postfoot("\bottomrule \end{tabular}") tex          ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_GuessingBehavior_raw.tex"  ///
	"${Replicate_SocialSignals_ATables}/Table_GuessingBehavior_Vaccines_InterYellow_FirstSecond.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_GuessingBehavior_raw.tex"



	* ----------------------------------------------------------------------------
	* outsheet p-values, without controls

	esttab  ///
	model_correct_35mo model_under_35mo model_over_35mo                 ///
	model_correct_9mo  model_under_9mo  model_over_9mo                  ///
	using "${Replicate_SocialSignals_ATables}/Table_GuessingBehavior_raw.tex", ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule "                         ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{6}{c}{\textbf{Number of vaccines of other children}}\\" ///
	"                                                 & \multicolumn{1}{c}{\textbf{Correct}} & \multicolumn{1}{c}{\textbf{Underestimate}} & \multicolumn{1}{c}{\textbf{Overestimate}} & \multicolumn{1}{c}{\textbf{Correct}}  & \multicolumn{1}{c}{\textbf{Underestimate}} & \multicolumn{1}{c}{\textbf{Overestimate}} \\" ///
	"\multicolumn{1}{l}{Vaccine take-up period}      & \multicolumn{3}{l}{3.5-9 months}  & \multicolumn{3}{l}{9-12 months} \\") ///
	scalars("C_mean             Mean Uninformative Bracelet "             ///
	"Obs               Observations"                             ///
	"controls          Controls"                                 ///
	"signal4_signal5   p(S4 Green = S5 Green)"             ///
	"signal4_ng_signal5_ng p(S4 Yellow = S5 Yellow)"       ///
	"UI_ng_signal4_ng  p(UI Yellow = S4 Yellow)"           ///
	"UI_ng_signal5_ng  p(UI Yellow = S5 Yellow)")          ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	order(3.intervention_arm                    4.intervention_arm        ///
	3.intervention_arm#1.yellow_bracelet  4.intervention_arm#1.yellow_bracelet  ///
	1.yellow_bracelet)                                              ///
	drop(_cons *dm*)  nomtitles                                          ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                       ///
	addnotes("") label se  nobaselevels noconstant noobs nolines          ///
	posthead(\midrule) postfoot("\bottomrule \end{tabular}") tex          ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_GuessingBehavior_raw.tex"  ///
	"${Replicate_SocialSignals_ATables}/Table_GuessingBehavior_Vaccines_InterYellow_FirstSecond.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_GuessingBehavior_raw.tex"



* **************************************************************************** *
* End of the Dofile !!!
* **************************************************************************** *
